#!/usr/bin/env bash
rm /bin/sh
ln -s /bin/bash /bin/sh
# shellcheck disable=SC2034
base_url=$(pwd)
begin_time=$(date +%s)
# 查看系统内核版本
kernel_release=$(uname -r)
release1=${kernel_release:0:1}
release2=${kernel_release:2:2}
if [[ (( ${release1} == 3 && ${release2} < 10 )) || (( ${release1} < 3 )) ]]; then
    echo "kernel_release->${kernel_release} < 3.10,Exited!!!"
    exit
fi
# shellcheck disable=SC2002
if [[ -e "/etc/centos-release" && "CentOS" == $(cat /etc/centos-release | awk '{print $1}') ]]; then
    yum remove -y docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-selinux \
                docker-engine
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager \
        --add-repo \
        http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    yum install -y https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.13-3.2.el7.x86_64.rpm
    yum install -y docker-ce-19.03.12
    yum install -y systemd
elif [[ -e "/etc/issue" && "Ubuntu" == $(cat /etc/issue | awk '{print $1}') ]]; then
    
    apt-get remove docker docker-engine docker.io containerd runc
    apt-get update
    apt-get install -y \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
    curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    apt-key fingerprint 0EBFCD88
    add-apt-repository \
       "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
      $(lsb_release -cs) \
      stable"
    apt-get update
    ubuntu_release=$(cat /etc/issue | awk '{print $2}')
    if (( ${ubuntu_release:0:2} >= 16 )); then
        apt-get install -y docker-ce docker-ce-cli containerd.io
    else
        apt-get install --install-recommends linux-generic-lts-xenial
        apt-get install -y docker-ce
        apt-get install -y systemd
    fi
else
    echo "Not CentOS or Ubuntu System！Exited!!!"
    exit
fi
systemctl start docker
systemctl enable docker
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-EOF
{
  "registry-mirrors": ["https://cm7mmu0c.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
echo "----------------------docker installed successfully！-----------------------"
finish_time=$(date +%s)
echo "Docker-init script run time:$((finish_time-begin_time))s"